/**
 * problem_014.c
 * Copyright (C) 2011-03-18 - xrose
 * Collatz problem
 */

#include <stdio.h>
#include <math.h>
#define maxsize 1000000

int collatz(unsigned int colz);
int main (int argc, char *argv[])
{
    register unsigned int i;
    int mcollatz = 0;
    unsigned int ncollatz;
    int tmp = 0;
    for(i=2; i<maxsize; i++)
    {
        tmp = collatz(i);
        if(mcollatz<tmp)
        {
            mcollatz = tmp;
            ncollatz = i;
        }
    }
    printf("Answer is mcollatz__%d__number__%d\n", mcollatz, ncollatz);
    return 0;
}

int collatz(unsigned int colz)
{
    //Input: a starting collatz number
    //Output: number of the collatz sequence
    register int count = 0;
    register unsigned long long n_colz = colz;

    do
    {
        count++;
        if(n_colz&1)
        {
            n_colz = 3*n_colz+1;
        } else
        {
            n_colz=n_colz>>1;
        }
    } while (n_colz>1);
    return count + 1;
}
